//
//  HttpPostRequest.m
//  网络请求(GET&POST) -> TZJ
//
//  Created by My Mac on 2017/4/13.
//  Copyright © 2017年 My Mac. All rights reserved.
//

#import "HttpPostRequest.h"

@implementation HttpPostRequest

+(void)postRequestWithURLString:(NSString *)urlString HttpBody:(NSMutableDictionary *)httpBody Handler:(void (^)(id _Nullable))value
{
    //网络地址
    NSURL *url = [NSURL URLWithString:urlString];
    //网络请求(默认GET请求)(POST请求要用可变的request:NSMutableURLRequest)
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //发送POST请求
    request.HTTPMethod = @"PosT";
    
    NSString *post = @"Post";
    NSLog(@"%@",[post uppercaseString]);
    NSLog(@"%@",post);
    //设置请求体数据
    request.HTTPBody = [NSJSONSerialization dataWithJSONObject:httpBody options:NSJSONWritingPrettyPrinted error:nil];
    //网络会话
    NSURLSession *session = [NSURLSession sharedSession];
    //创建任务
    NSURLSessionDataTask *sdTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        //更新UI
        dispatch_async(dispatch_get_main_queue(), ^{
            //回调
            value(dict);
        });
    }];
    
    [sdTask resume];
    
}

@end
